"""
    运行环境搭建
    pip install flask
    pip install flask-cors  # 处理跨域问题

    python login_api.py
"""

from flask import Flask, request, jsonify
from flask_cors import CORS
import json

app = Flask(__name__)
CORS(app)  # 允许跨域请求

@app.route('/api/user/login', methods=['POST'])
def login():
    try:
        # 获取请求数据
        data = request.get_json()

        username = data.get('username')
        password = data.get('password')

        # 简单验证
        if username == 'admin' and password == '123456':
            response_data = {
                'code': 200,
                'msg': '登录成功',
                'data': {
                    'tokenName': 'token',
                    'tokenValue': '241acc83-6766-4ceb-8ec0-5922cc417061',
                    'isLogin': True,
                    'loginId': username,
                }
            }
        else:
            response_data = {
                'code': 401,
                'msg': '用户名或密码错误',
                'data': None
            }

        return jsonify(response_data)

    except Exception as e:
        return jsonify({
            'code': 500,
            'message': '服务器内部错误',
            'data': None
        }), 500

# 添加以下函数来打印所有路由信息
def print_routes():
    print("已注册的接口:")
    for rule in app.url_map.iter_rules():
        methods = ','.join(sorted(rule.methods))
        print(f"{rule.endpoint:20s} {methods:20s} {rule}")

if __name__ == '__main__':
    # 启动时打印所有接口信息
    with app.app_context():
        print_routes()
    app.run(host='0.0.0.0', port=8080, debug=True)
